//
//  OrderManager.m
//  Menu
//
//  Created by trimad on 11-11-11.
//  Copyright 2011 Trimad Group. All rights reserved.
//

#import "OrderManager.h"

#import "ContextManager.h"
#import "DisplayManager.h"
#import "Item.h"
#import "MenuViewController.h"
#import "Order.h"
#import "OrderItem.h"
#import "Util.h"

@interface OrderManager(Private)
-(void)notifyDidChangeOrderItem:(OrderItem *)orderItem;
-(void)notifyDidRemoveOrderItem:(OrderItem *)orderItem;
@end


@implementation OrderManager

@synthesize orderHistory;
@synthesize currentOrder;

static OrderManager *_instance = nil;
NSMutableArray *registeredDelegate = nil;

+ (OrderManager *)getInstance {
	if (!_instance) {
		@synchronized(self) {
			if (!_instance) {
				_instance = [[OrderManager alloc] init];
			}
		}
	}
	return _instance;
}

- (id)init {
	self = [super init];
	if (self != nil) {
		orderHistory = [[NSMutableArray alloc] initWithCapacity:10];
		currentOrder = [[Order alloc] init];
		registeredDelegate = [[NSMutableArray alloc] initWithCapacity:10];
	}
	return self;
}

- (void)finishCurrentOrder {
	if (currentOrder == nil) {
		return;
	}
	[orderHistory addObject:currentOrder];
	[currentOrder release];
}

/* 用updateOrderItem来概括add和remove功能 */
- (void)updateOrderItem:(OrderItem *)orderItem {
	[currentOrder updateOrderItem:orderItem];
	[self notifyDidChangeOrderItem:orderItem];
	NSString *tempQuantity=[NSString stringWithFormat:@"%i",orderItem.quantity];
	NSString *tempPrice=[NSString stringWithFormat:@"%.2f",orderItem.item.price];
	NSString *tempPageIndex=[NSString stringWithFormat:@"%i",[[DisplayManager instance] pageIndex:orderItem.item.code]];
	NSString *tempFlavor=@"";
	NSMutableArray *tempArray=[[NSMutableArray alloc] initWithObjects:orderItem.item.name, tempQuantity, tempPrice, tempPageIndex,tempFlavor,nil];
	[tempQuantity release];
	[tempPrice release];
	[tempPageIndex release];
	NSMutableDictionary *tempDic=[Util readFileForDictionary:@"localOrderData"];
	if(tempDic==nil){
		tempDic=[[NSMutableDictionary alloc] init];
	}
	[tempDic setValue:tempArray forKey:orderItem.item.code];
	if([tempQuantity intValue] == 0){
		[tempDic removeObjectForKey:orderItem.item.code];
	}
	[Util writeFileForDictionary:tempDic:@"localOrderData"];
	MenuViewController *menuViewController=[[ContextManager getInstance] rootViewController];
	menuViewController.orderInHandView.hidden=YES;
}

-(OrderItem *)getOrderItem:(NSString *)itemCode {
	return [currentOrder getOrderItem:itemCode];
}

- (OrderItem *)removeOrderItem:(NSString *)itemCode {
	OrderItem * orderItem = [currentOrder removeOrderItem:itemCode];
	[self notifyDidRemoveOrderItem:orderItem];
	return orderItem;
}

-(void)addDelegate:(id)delegate {
	if (![registeredDelegate containsObject:delegate]) {
		[registeredDelegate addObject:delegate];
	}
}

-(void)notifyDidChangeOrderItem:(OrderItem *)orderItem {
	for (OrderManagerDelegate *aDelegate in registeredDelegate) {
		[aDelegate didUpdateOrderItem:orderItem];
	}
}

-(void)notifyDidRemoveOrderItem:(OrderItem *)orderItem {
	for (OrderManagerDelegate *aDelegate in registeredDelegate) {
		[aDelegate didRemoveOrderItem:orderItem];
	}
}


@end